import traceback
from flask import request, g

from common.utils.jwt_utils import verify_jwt


def jwt_authentiction():
    """
    前置登录

    解析token
    1判断token是否存在    存在则校验  不存在不管
    2校验通过存到

    """
    g.user_id = None
    g.is_refresh = False
    token = request.headers.get('Authorization')
    if token and token.startswith('Bearer '):
        token = token[7:]
        payload = verify_jwt(token)
        try:
            if payload:
                g.user_id = payload.get('user_id')
                g.is_refresh = payload.get('is_refresh', False)
        except:
            error = traceback.format_exc()
            print('error{}'.format(error))
